package com.zhentao.interceptor;

import com.zhentao.model.common.pojo.management.UserInfo;
import com.zhentao.userLocal.AdminUserThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import utils.JwtService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: LoginInterceptor
 * @Author: 振涛教育_Q
 * @Date: 2024/11/18 9:21
 */
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");

        UserInfo userInfo = new UserInfo();
        Map<String, Object> map = JwtService.getClaimsMap(token);
        if (map != null) {
            Object id = map.get("id");

            userInfo.setId(Integer.parseInt(id.toString()));
            userInfo.setUserName(map.get("userName") + "");


            AdminUserThreadLocalUtils.setUserInfo(userInfo);
        }



        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
